package com.tnovoselec.android.common.notification;

import android.app.AlarmManager;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.text.TextUtils;

import com.tnovoselec.android.common.util.ApplicationUtil;
/**
 * Title: Framework <br />
 * Copyright: Copyright (c) 2011 <br />
 * 
 * The NTHLocalPushService is used to simulate push notifications. It actually uses a pull mechanism, 
 * by periodically checking with server if any news are available. 
 * 
 * @author Tomislav Novoselec
 * @version 1.0.0
 * @since 1.0.0
 */
public abstract class MyLocalPushService extends IntentService{

    public MyLocalPushService(String name) {
	super(name);
    }
    /**
     * 
     * @return Title to be shown in notification bar
     */
    public abstract String getNotificationTitle();
    /**
     * 
     * @return Text to be shown in notification
     */
    public abstract String getNotificationText();
    /**
     * 
     * @return time in millis which represents interval in which servise is executed
     */
    public abstract long getInterval();
    /**
     * 
     * @return flag if service should execute or not
     */
    public abstract boolean isPushEnabled();
    /**
     * 
     * @return Intent which will be launched when notification is selected
     */
    public abstract Intent getIntentWithFlags();
    /**
     * 
     * @return id of drawable which will be shown in notification bar
     */
    public abstract int getIconDrawable();
    /**
     * 
     * @return id of refresh intent
     */
    public abstract String getActionRefresh();
    /**
     * 
     * @return id of start intent
     */
    public abstract String getActionStart();
    
    @Override
    protected void onHandleIntent(Intent intent) {

	if (isPushEnabled() == true && ApplicationUtil.isOnline(getApplicationContext())) {

	    if (getActionRefresh().equals(intent.getAction()) || getActionStart().equals(intent.getAction())) {

		String notificationText = getNotificationText();
		if (!TextUtils.isEmpty(notificationText)) {
		    
		    Intent i = getIntentWithFlags();

		    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);

		    Notification notification = new Notification(getIconDrawable(), getNotificationTitle(), System.currentTimeMillis());

		    notification.defaults |= Notification.DEFAULT_ALL;
		    notification.ledARGB = 0xffff00ff;
		    notification.flags |= Notification.FLAG_AUTO_CANCEL;
		    notification.setLatestEventInfo(getApplicationContext(), getNotificationTitle(), notificationText, contentIntent);

		    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		    notificationManager.notify(0, notification);

		}
	    }
	}

	AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
	if (alarmManager != null) {
	    Intent i = new Intent(getActionRefresh());
	    PendingIntent pendingIntent = PendingIntent.getService(this, 0, i, 0);
	    alarmManager.cancel(pendingIntent);
	    if (isPushEnabled() == true) {
		alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + getInterval(), pendingIntent);
	    }
	}
	stopSelf();
    }
}
